package com.sl.au.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.KeyContact;
import com.sl.au.entity.M2R;
import com.sl.au.entity.Role;
import com.sl.au.service.M2RService;
import com.sl.au.service.RoleService;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value="/ui/role")
public class RoleController extends BaseController{

    @Autowired
    RoleService roleService;
    
    @Autowired
    M2RService m2rService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView modelAndView=createLayoutView("role/roleindex");
        return modelAndView;

    }
    @SuppressWarnings("rawtypes")
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
    public Map getPage(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        String key=request.getParameter("key");

        List<Role> roles=null;
        if(org.springframework.util.StringUtils.hasText(key)){
            roles = roleService.getRoles(start, length, key, null, null);}
        else {
            roles = roleService.getPage(start, length, null, null, null);
        }
        Long count = roleService.getRepository().count();
        map.put("data", roles);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }
    
     ///验证角色是否重复
  		@RequestMapping(value="rolevalidate",method = RequestMethod.GET)
  		public Map<String, Object> codeValidate(HttpServletRequest request){
  			Map<String, Object> map=new HashMap<String, Object>();
  			String name=request.getParameter("name");
  			long count=roleService.getRepository().getRoleCount(name);
  			if(count>0){
  				map.put("code", 0);	
  			}else{
  				map.put("code", 1);
  			}
  			return map;
  		}
    //删除角色和配置的模块
  		@RequestMapping(value = "delRole", method = RequestMethod.GET)
  		public Map<String, Object> delKeyContact(HttpServletRequest request) {
  			Map<String, Object> map = new HashMap<String, Object>();
  			String id = request.getParameter("id");
  			Role r=roleService.getRepository().findOne(id);
  			List<M2R> m2rs=m2rService.getRepository().findByRole(r);
  			m2rService.getRepository().deleteInBatch(m2rs);
  			roleService.getRepository().delete(r);
  			map.put("code", 0);
  			return map;
  		}	
  		
}
